package cglib.interfacedemo.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

public class ActorProxy implements MethodInterceptor {

    private Object orientObject;

    public Object newProxyInstance(Object targetObject){
        this.orientObject = targetObject;
        Enhancer enhancer = new Enhancer();
        enhancer.setInterfaces(targetObject.getClass().getInterfaces());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    //演戏之前要做的事情
    private void beforeAct(){
        System.out.println("洽谈档期..");
        System.out.println("洽谈片酬..");
        System.out.println("洽谈广告合作..");
    }

    //演戏之后要做的事情
    private void afterAct(){
        System.out.println("洽谈首映仪式..");
        System.out.println("洽谈颁奖典礼..");
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object ret = null;
        this.beforeAct();
        //调用成龙真正的演戏方法
        ret =method.invoke(this.orientObject,objects);
        this.afterAct();
        return ret;
    }
}
